package com.example.lcode.binary;

public class Question33 {

    public int search(int[] nums, int target) {
        return searchTarget(nums, 0, nums.length - 1, target);
    }

    private int searchTarget(int[] nums, int left, int right, int target) {
        if (left > right) {
            return -1;
        }
        int mid = (left + right) / 2;
        if (nums[mid] == target) {
            return mid;
        } else if (nums[left] <= nums[mid]) {
            if (nums[left] <= target && nums[mid] >= target) {
                return searchTarget(nums, left, mid - 1, target);
            } else {
                return searchTarget(nums, mid + 1, right, target);
            }
        } else{
            if (nums[mid] <= target && nums[right] >= target) {
                return searchTarget(nums, mid + 1, right, target);
            } else {
                return searchTarget(nums, left, mid - 1, target);
            }
        }
    }

    public static void main(String[] args) {
        Question33 question33 = new Question33();
        int []arr = {5,1,3};
        question33.search(arr, 5);
    }

}
